package com.example.diyview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class stpeView extends View {
    private int number = 10;
    private int numberSize = 10;
    private int outColor = Color.BLUE;
    private int innerColer = Color.GRAY;
    private int ringBord = 10;
    private Paint mpaint;
    private Paint outpaint;
    int now;
    public synchronized void setNow(int now){
        this.now = now;
        invalidate();
    }
    public synchronized void setNumber(int number){
        this.number = number;
    }

    public stpeView(Context context) {
        this(context,null);
    }

    public stpeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public stpeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.stpeView);
        outColor =typedArray.getColor(R.styleable.stpeView_outColor,outColor);
        ringBord = (int) typedArray.getDimension(R.styleable.stpeView_ringBord,pTosp(ringBord));
        number = typedArray.getInt(R.styleable.stpeView_number,number);
        numberSize = typedArray.getDimensionPixelSize(R.styleable.stpeView_numberSize,pTosp(numberSize));
        typedArray.recycle();
        mpaint = new Paint();
        outpaint = new Paint();
        outpaint.setAntiAlias(true);
        mpaint.setAntiAlias(true);
        mpaint.setStrokeWidth(ringBord);
        mpaint.setStyle(Paint.Style.STROKE);
        mpaint.setColor(outColor);
        mpaint.setStrokeCap(Paint.Cap.ROUND);
        outpaint.setTextSize(numberSize);
    }

    private int pTosp(int size) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,size,getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);   //二进制32位  模式2位 值30位
        int wideMode = MeasureSpec.getMode(widthMeasureSpec);
        int heihtMode = MeasureSpec.getMode(heightMeasureSpec);
//        MeasureSpec.AT_MOST wrapcontent
//        MeasureSpec.EXACTLY 确切值  100dp  match  fill
//        MeasureSpec.UNSPECIFIED 尽可能的大 listview scollview
        int wideth = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if(wideMode == MeasureSpec.AT_MOST)
        {
            wideth = getPaddingLeft() + getPaddingRight();
        }
        if(heihtMode == MeasureSpec.AT_MOST)
        {
            height = getPaddingTop() + getPaddingBottom();
        }
        if(wideth>=height) wideth=height;
        else  height=wideth;
        setMeasuredDimension(wideth,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas); //描边是存在宽度,边会出界 ,要考虑边宽
        RectF rectF = new RectF(ringBord/2,ringBord/2,getWidth()-ringBord/2,getHeight()-ringBord/2);
//        canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC);
        now+=number/100;
        String n =now+"";
        Rect rect = new Rect();
        outpaint.getTextBounds(n,0,n.length(),rect);
        canvas.drawArc(rectF, 135, ((float) now/number)*270, false, mpaint);
        canvas.drawText(n,getWidth()/2-rect.width()/2,getHeight()/2,outpaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_UP:;
            case MotionEvent.ACTION_DOWN:;
            case MotionEvent.ACTION_HOVER_MOVE:;
        }
        return super.onTouchEvent(event);
    }
}
